//
//  GPUserProfileController.m
//  Keping
//
//  Created by 23 on 2017/12/7.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "GPUserProfileController.h"
#import "GPContactRequest.h"
#import "GPContactRequest.h"
#import "GPAlterAliasController.h"

@interface GPUserProfileController ()

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *mobileLabel;
@property (weak, nonatomic) IBOutlet UILabel *aliasLabel;
@property (weak, nonatomic) IBOutlet UIView *aliasView;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UISwitch *blockButton;


@end

@implementation GPUserProfileController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.title = @"Profile";
    [self.aliasView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alterAlias)]];
    
    
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self loadData];
}


- (void)loadData
{
    [self showLoading];
    NSString *url = [@"/intelligent-community/api/biz/ou/v1/partys/getPhone/{phone}.htm" stringByReplacingOccurrencesOfString:@"{phone}" withString:self.phone];
    GPContactRequest *request = [[GPContactRequest alloc] initWithURL:url];
    [self showLoading];
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        [self hideHUD];
        NSDictionary *dict = request.responseJSONObject;
        NSDictionary *partyView = dict[@"partyView"];
        NSLog(@">>>>>>>>>>>>>smallyou:获取用户消息--->%@", partyView);
        if (partyView == nil) {
            return;
        }
        
        //更新UI
        self.titleLabel.text = partyView[@"nickName"];
        self.phoneLabel.text = [NSString stringWithFormat:@"Phone: %@", partyView[@"phone"]];
        self.nameLabel.text = [NSString stringWithFormat:@"Name: %@", partyView[@"realName"]];
        self.nameLabel.text = [NSString stringWithFormat:@"Name: %@", partyView[@"nickName"]];
        self.mobileLabel.text = partyView[@"phone"];
        self.aliasLabel.text = partyView[@"remarks"];
        self.addressLabel.text = partyView[@"communityName"];
        self.blockButton.on = [partyView[@"isBlock"] isEqualToString:@"N"]?NO:YES;
        
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        NSLog(@">>>>>>>>>>>>>smallyou:获取用户消息失败--->");
    }];
    
//    if (self.uid == nil ||self.uid.length == 0) {
//        [self showText:@"Current User Is Not Exist"];
//        return;
//    }
//    NSString *url = [kIMGetBuddyInfoPath stringByReplacingOccurrencesOfString:@"{friendId}" withString:self.uid];
//    GPContactRequest *request = [[GPContactRequest alloc] initWithURL:url];
//    [self showLoading];
//    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
//
//        [self hideHUD];
//
//        NSDictionary *dict = request.responseJSONObject;
//
//        if ([dict[@"statusCode"] integerValue] != 200) {
//
//            [self showErrorText:dict[@"msg"] errCode:[dict[@"statusCode"] integerValue]];
//            return;
//        }
//
//        NSDictionary *partyView = dict[@"relationFriendView"];
//
//        //更新UI
//        self.titleLabel.text = partyView[@"nickName"];
//        self.phoneLabel.text = [NSString stringWithFormat:@"Phone: %@", partyView[@"phone"]];
//        self.nameLabel.text = [NSString stringWithFormat:@"Name: %@", partyView[@"realName"]];
//        self.nameLabel.text = [NSString stringWithFormat:@"Name: %@", partyView[@"nickName"]];
//        self.mobileLabel.text = partyView[@"phone"];
//        self.aliasLabel.text = partyView[@"remarks"];
//        self.addressLabel.text = partyView[@"communityName"];
//        self.blockButton.on = [partyView[@"isBlock"] isEqualToString:@"N"]?NO:YES;
//
//
//        NSLog(@"---->smallyou: %@", request.responseJSONObject);
//
//    } failure:^(__kindof KBaseRequest * _Nonnull request) {
//
//    }];
}


- (IBAction)blockSwitchValueChanged:(UISwitch *)sender {
    
    BOOL on = sender.isOn;
    
    if (on) {
        WEAKSELF
        //加入黑名
        UIAlertController *vc = [UIAlertController alertControllerWithTitle:nil message:@"Are you sure you want to unblock this contact?" preferredStyle:UIAlertControllerStyleAlert];
        [vc addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [weakSelf blockUser:on completion:^(BOOL success) {
                if (!success) {
                    sender.on = !sender.isOn;
                }
            }];
            
            
        }]];
        [vc addAction:[UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            sender.on = !sender.isOn;
        }]];
        [self presentViewController:vc animated:YES completion:nil];
    }else{
        //取消加入黑名单
    
        [self blockUser:on completion:^(BOOL success) {
            if (!success) {
                sender.on = !sender.isOn;
            }
        }];
        
    }
    
    
    
}

/**加入和名单*/
- (void)blockUser:(BOOL)on completion:(void(^)(BOOL success))completion
{
    if (self.uid.length == 0) {
        [self showText:@"Current User Is Not Exist"];
        return;
    }
    NSString *url = [kIMAlterUserInfoURL stringByReplacingOccurrencesOfString:@"{userId}" withString:self.uid];
    GPContactRequest *request = [[GPContactRequest alloc] initWithURL:url];
    NSString *isBlock = on?@"Y":@"N";
    request.isPost = YES;
    request.params = @{@"isBlock":isBlock};
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        
        NSDictionary *response = request.responseJSONObject;
        
        if ([response[@"statusCode"] integerValue] != 200) {
            
            [self showErrorText:response[@"msg"] errCode:[response[@"statusCode"] integerValue]];
            if (completion) {
                completion(NO);
            }
            return;
        }
        
        if (completion) {
            completion(YES);
        }
        
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        
        if (completion) {
            completion(NO);
        }
        
    }];
}


/**修改别名*/
- (void)alterAlias
{
    
    GPAlterAliasController *vc = [[GPAlterAliasController alloc] init];
    vc.userId = self.uid;
    vc.placeholder = self.aliasLabel.text;
    [self.navigationController pushViewController:vc animated:YES];
}


@end
